package main

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/db1")
	defer db.Close()

	//然后通过tx操作Exec, Query, QueryRow and Prepare 方法，流程和上面一样。示例
	tx, err := db.Begin()
	stmt, err := tx.Prepare(`UPDATE miao SET cnt = ?, version = ? WHERE id = ?`)
	_, err = stmt.Exec(5, 3, 4)
	//执行出现错误，回滚
	if err != nil {
		tx.Rollback()
		panic(err)
	}
	//执行正常，提交
	err = tx.Commit()
	//提交失败，panic
	if err != nil {
		tx.Rollback()
		panic(err)
	}
}
